class ProductsController < ApplicationController
  # GET /products
  # GET /products.xml
  def index
    @products = Product.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @products }
    end
  end

  # GET /products/1
  # GET /products/1.xml
  def show
    @product = Product.find(params[:id])
    @farm = Farm.find(:all)
    @national = National.find(:all)
    @origin = Productorigin.find(:all, :conditions => ["product_id = ?", params[:id]])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/new
  # GET /products/new.xml
  def new
    @product = Product.new
    @farm = Farm.find(:all)
    @national = National.find(:all)
    @saletype = Commoncode.find(:all, :conditions=>["parentcode= 'ST'"])
    @price = Commoncode.find(:all, :conditions=>["parentcode = 'PR'"])
    
    @product.productorigins.build
    @product.prices.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
    @farm = Farm.find(:all)
    @national = National.find(:all)
    @saletype = Commoncode.find(:all, :conditions=>["parentcode= 'ST'"])
    @price = Commoncode.find(:all, :conditions=>["parentcode = 'PR'"])
  end

  # POST /products
  # POST /products.xml
  def create
    @product = Product.new(params[:product])  
    respond_to do |format|
      if @product.save
        flash[:notice] = 'Product was successfully created.'
        format.html { redirect_to(@product) }
        format.xml  { render :xml => @product, :status => :created, :location => @product }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.xml
  def update
    params[:product][:existing_productorigin_attributes] ||= {}
    params[:product][:existing_price_attributes] ||= {}

    @product = Product.find(params[:id])
    @nation = National.find(:all)
    @farm = Farm.find(:all)
     @saletype = Commoncode.find(:all, :conditions=>["parentcode= 'ST'"])
    @price = Commoncode.find(:all, :conditions=>["parentcode = 'PR'"])
    respond_to do |format|
      if @product.update_attributes(params[:product])
        flash[:notice] = 'Product was successfully updated.'
        format.html { redirect_to(@product) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.xml
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(products_url) }
      format.xml  { head :ok }
    end
  end
end
